<?php
/**
 * <b>RUBIK IT Solution & Development ltd.</b>
 *
 * PROJECT		: Rubik Ultimate Framework
 * @version 	: 1.1
 * COPYRIGHT    : 2008
 * ------------------------------------------------------
 * 
 * Created on	:  Jul 14, 2008
 * 
 */

// no direct access
defined ( '_FW_EXEC' ) or die ( 'Restricted access' );

RCoreImporter::cls( "base.DateTimeParser" );

class RDateTimeHelper {

	/**
	 * Get the diff from the current string.
	 * Example:
	 * - now 10:30PM input is: 10:5PM return: 25 minutes.
	 * - now 2009.10.10 10:30PM input is: 2009.10.5 10:5PM return: 2009.10.5.
	 * 
	 * @param unknown_type $strDate
	 * @return unknown_type
	 */
	static function getDateDiffString($strDate) {
		$now = time();
		$added = strtotime($strDate);
		$diff = $now - $added;
		
		if ($diff > 12 * 3600 * 7) {
			return date('Y-m-d', $added);
		} else if ($diff > 60) {
			$h = intval($diff / 3600);
			if ($h >= 24) {
				$days = intval($h/24);
				if ($days > 1) {
					return "$days days ago";
				}
				else {
					return "$days day ago";
				}
			}
			$min = intval(($diff % 3600) / 60);
			if ($h) {
				$time .= "$h hour"; 
			}
			if ($min) {
				if ($min > 1) {
					$time .= " {$min} minutes";
				}
				else {
					$time .= " {$min} minute";
				}
			}
			$time .= " ago";
		}
		else {
			$time = "Has just cached";
		}
		
		return $time;
	}
	
	/**
	 * Get the time stamp of the input data (with formated by $dateFormat)
	 *
	 * @param string $data date data
	 * @param string $dateFormat format of date
	 * @param boolean $localize 
	 * @return int timestamp
	 */
	static function getTimeStamp($data, $dateFormat, $localize = false) {
		$paser = new DateTimeParser ( );
		$result = $paser->parseDate ( $dateFormat, $data, $localize );
		return mktime ( $result ["Hour"], $result ["Minute"], $result ["Second"], $result ["Month"], $result ["Day"], $result ["Year"] );
	}
	
	/**
	 * Reformats the given date as same as the first date() static function variable.
	 *
	 * @param string $patternTo
	 * @param string $patternFrom
	 * @param string $date
	 * @return string
	 */
	static function reFormat($patternTo, $patternFrom, $date) {
		if (empty ( $patternTo )) {
			if (empty ( $patternFrom )) {
				return $date;
			}
			return RDateTimeHelper::getTimeStamp ( $date, $patternFrom );
		}
		if (empty ( $patternFrom )) {
			return date ( $patternTo, $date );
		}
		
		$paser = new DateTimeParser ( );
		return $paser->Format ( $patternTo, $patternFrom, $date );
	}
	
	/**
	 * Check if the date is parsed or not
	 *
	 * @param string $pattern
	 * @param string $date
	 * @return boolean
	 */
	static function isParsed($pattern, $date) {
		$paser = new DateTimeParser ( );
		return $paser->isParsed ( $pattern, $date );
	}
	
	/**
	 * Formatted time-difference between $t1 and $t2 (seconds)
	 * The difference in seconds is distributed into years, months, etc. as specified by $format.
	 * $format is a string containing characters y, f, w, d, h, m or s, for years, months, weeks, days, hours, minutes or seconds, resp.
	 * Output is an array containing keys y, f, etc., with the numerical values.
	 * Values are negative if $t1 is more than $t2.
	 * @param timestamp $t1
	 * @param timestamp $t2
	 * @param string $format
	 * @return array
	 * 
	 */
	static function formatTimeStampDiff($t1, $t2 = null, $format = 'yfwdhms') {
		$t2 = $t2 === null ? time () : $t2;
		$s = abs ( $t2 - $t1 );
		$sign = $t2 > $t1 ? 1 : - 1;
		$out = array ();
		$left = $s;
		$format = array_unique ( str_split ( preg_replace ( '`[^yfwdhms]`', '', strtolower ( $format ) ) ) );
		$format_count = count ( $format );
		$a = array ('y' => 31556926, 'f' => 2629744, 'w' => 604800, 'd' => 86400, 'h' => 3600, 'm' => 60, 's' => 1 );
		$i = 0;
		foreach ( $a as $k => $v ) {
			if (in_array ( $k, $format )) {
				++ $i;
				if ($i != $format_count) {
					$out [$k] = $sign * ( int ) ($left / $v);
					$left = $left % $v;
				} else {
					$out [$k] = $sign * ($left / $v);
				}
			} else {
				$out [$k] = 0;
			}
		}
		return $out;
	}
}
?>